"""
    Controller for office table. Contains print timetable mass action.
"""

from turbogears import expose, validators, widgets
from mnemoply import model
from mnemoply.mass_actions import MassAction, MassActionForm
from mnemoply.controllers import Report
from mnemoply.controllers.lookup import LookupController, TableCol, SQLObjectLookupDataSource
from mnemoply.controllers.crud import CRUDController, ItemActionsTab
from mnemoply.utils import getMondayBeforeThat, objs2ids

class ClassroomsItemTab(ItemActionsTab):
    title = _("Classrooms")
    name = "classrooms_tab"
    ma_class_name = 'Classroom'

    def getRecords(self, obj):
        agrs = list(model.Classroom.selectBy(office=obj))
        agrs.sort(key=lambda x: x.title)
        return agrs

    def getRecordTitle(self, obj):
        return obj.title

class MAPrintTimetable(MassAction):
    """
        Show timetable for selected offices.
    """

    target_class = model.Office
    name = "print_timetable"
    title = _("Show timetable")

    form = MassActionForm(
        name=name,
        fields=[
            widgets.CalendarDatePicker(
                label = _('Date'),
                name = 'date',
                calendar_lang = 'ru',
                format = '%d.%m.%Y',
                validator = validators.DateTimeConverter(format="%d.%m.%Y")),
            ]
        )

    @expose('genshi:mnemoply.templates.print_timetable')
    def call(self, objs, date):
        return dict(offices=objs, start_date=getMondayBeforeThat(date))

class MAPrintTimetableToRDS(MassAction):
    """
        Print timetable for selected offices.
    """

    target_class = model.Office
    name = "print_timetable_rds"
    title = _("Print timetable")

    read_only = True

    form = MassActionForm(
        name=name,
        fields=[
            widgets.CalendarDatePicker(
                label = _('Date'),
                name = 'date',
                calendar_lang = 'ru',
                format = '%d.%m.%Y',
                validator = validators.DateTimeConverter(format="%d.%m.%Y")),
            ]
        )

    def call(self, objs, date):
        return Report.generate('PrintTimetable', ids=objs2ids(objs), start_date=getMondayBeforeThat(date))

class OfficeController(LookupController, CRUDController):
    model_object = model.Office
    item_tabs = [ClassroomsItemTab()]
    lookup_columns = [
        TableCol(name='name', caption=_('Name')),
        TableCol(name='ordinal', caption=_('Ordinal'), validator=validators.Int()),
        TableCol(name='address', caption=_('Address')),
        TableCol(name='abbreviation', caption=_('Abbreviation')),
    ]
    view_fields = ['name', 'ordinal', 'address', 'abbreviation', 'comment']

    def __init__(self):
        LookupController.__init__(self,
            data_source=SQLObjectLookupDataSource(self.model_object),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
            mass_actions_class=self.model_object
        )
        CRUDController.__init__(self, model_object=self.model_object)

exposed = OfficeController()
